package com.encryption.service.ca.impl;

import java.io.File;
import java.io.FileOutputStream;
import java.security.NoSuchAlgorithmException;
import java.security.cert.Certificate;
import java.security.cert.CertificateEncodingException;
import java.security.cert.X509Certificate;
import java.util.List;

import com.encryption.dao.DeptCertDAO;
import com.encryption.model.Department;
import com.encryption.model.DeptCert;
import com.encryption.service.ca.InitDeptCertInfo;
import com.encryption.service.ca.PKCS12CertificateGenerator;
import com.encryption.service.ca.util.CAConstants;
import com.encryption.service.ca.util.CryptoUtil;
import com.encryption.service.ca.util.PropertyLoader;

/**
 * @author yitao
 * @version $Id: InitDeptCertInfoImpl.java,v 0.1 2012-11-18 ����04:50:14 user
 *          Exp $
 */
public class InitDeptCertInfoImpl implements InitDeptCertInfo {

	private DeptCertDAO deptCertDAO = new DeptCertDAO();
	private PKCS12CertificateGenerator gen = new PKCS12CertificateGeneratorImpl();

	public Boolean init() {

		Certificate cert = null;
		List<DeptCert> dcs = deptCertDAO.findAll();

		if (dcs == null || dcs.isEmpty()) {
			try {
				cert = gen.generate("001",
						PropertyLoader.get(CAConstants.CORP_NAME), "");

				File root_file = new File(
						PropertyLoader.get(CAConstants.CERT_PATH) + "\\"
								+ PropertyLoader.get(CAConstants.CORP_NAME)
								+ ".cer");
				FileOutputStream fos = null;
				if (!root_file.exists()) {

					fos = new FileOutputStream(root_file);
					fos.write(cert.getEncoded());
					fos.close();
				}
			} catch (Exception e) {

			}
			String thrumbId = null;
			try {
				if (cert == null)
					return false;
				thrumbId = CryptoUtil.getThumbPrint((X509Certificate) cert);
			} catch (CertificateEncodingException e) {
				throw new RuntimeException(
						"get cert thrumbId fail CertificateEncodingException");
			} catch (NoSuchAlgorithmException e) {
				throw new RuntimeException(
						"get cert thrumbId fail NoSuchAlgorithmException");
			}
			DeptCert deptCert = new DeptCert();
			deptCert.setCertSn("");
			deptCert.setCertThrumbid(thrumbId);
			deptCert.setDeptId("001");
			deptCertDAO.save(deptCert);
		}
		return true;
	}

	@Override
	public Boolean addDeptCert(Department dept) {
		Certificate cert = null;
		try {
			cert = gen.generate(dept.getId() + "", dept.getName() + "", "");

		} catch (Exception e) {
			throw new RuntimeException("生成部门证书错误");
		}
		String thrumbId = null;
		try {
			thrumbId = CryptoUtil.getThumbPrint((X509Certificate) cert);
		} catch (CertificateEncodingException e) {
			throw new RuntimeException(
					"get cert thrumbId fail CertificateEncodingException");
		} catch (NoSuchAlgorithmException e) {
			throw new RuntimeException(
					"get cert thrumbId fail NoSuchAlgorithmException");

		}
		DeptCert deptCert = new DeptCert();
		deptCert.setCertSn("");
		deptCert.setCertThrumbid(thrumbId);
		deptCert.setDeptId(dept.getId() + "");
		deptCertDAO.save(deptCert);
		return true;

	}

}
